package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.tools.Constants;

/**
 * @author lemonjesnme
 * @category my_story
 */
public class C2S3returnStory extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:"+Constants.DBNAME);
		DB.connDB();
		super.init();
	}
	
	@SuppressWarnings("deprecation")
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println(request.getCharacterEncoding());
		System.out.println("[vswb3] whitebox3 get begin");
		PrintWriter writer = response.getWriter();
		String user_id = null;
		String my_story = null;
		String time = null;
		String deletepeople = null;
		
		while (er.hasMoreElements()) {
			String element = er.nextElement();

			/**
			 * return the friends table primay id
			 */
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
				System.out.println("[vswb3] user_id:" + user_id);
			}

			if (element.equals("my_story")) {
				my_story = request.getParameterValues("my_story")[0];
				System.out.println("[vswb3] my_story into user_id:" + user_id);
			}
			if (element.equals("deletepeople")) {
				deletepeople = request.getParameterValues("deletepeople")[0];
				System.out.println("[vswb3] deletepeople user_id:" + deletepeople);
			}
			if (element.equals("time")) {
				time = request.getParameterValues("time")[0];
				if(time.equals("all")){
					
				}
				else
				{
					time = "time";
				}
			}
		}
		
		if(deletepeople != null){
			deletepeople = deletepeople.trim();
			DB.delete("delete from buy where user_id='"+deletepeople+"';");
			DB.delete("delete from config where user_id='"+deletepeople+"';");
			DB.delete("delete from event where e_user_id='"+deletepeople+"';");
			DB.delete("delete from friends where _id='"+deletepeople+"' or user_id='"+deletepeople+"';");
			DB.delete("delete from user where _id='"+deletepeople+"';");
			DB.delete("delete from userinfo where user_id='"+deletepeople+"';");
			writer.println("delete user:"+deletepeople+" ok");
			System.out.println("delete user:"+deletepeople+" ok");
		}
		
		if ((my_story != null) && (user_id != null)) {
			ResultSet rs = DB.select("UserInfo", "my_story", "user_id", user_id, null);
			try {
				while (rs.next()) {
					my_story = rs.getString("my_story");
					writer.println(my_story);
					System.out.println("[vswb3] my_story:" + my_story);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if ((time != null) && (user_id != null) && (time.equals("time"))) {
			ResultSet rs1 = DB.select("UserInfo", "time", "user_id", user_id, null);
			try {
				while (rs1.next()) {
					time = rs1.getString("time");
					writer.println(time);
//					System.out.println("[vswb3] time:" + time);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if ((time != null) && time.equals("all")) {
			String nick_name = "";
			ResultSet rs1 = DB.select("select user_id,phone_number,time,my_story from userinfo,user where user._id=userinfo.user_id order by time desc;");
			try {
				while (rs1.next()) {
					user_id = rs1.getString("user_id");
					time = rs1.getString("time");
					my_story = rs1.getString("my_story");
					nick_name = rs1.getString("phone_number");
					writer.println(nick_name+"莪"+my_story+"峩"+time+"僫"+user_id+"~");
					
				}
				System.out.println("正在使用版本 >10");
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					rs1.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		DB.connDB();
		System.out.println(request.getCharacterEncoding());
		System.out.println("[vswb3] whitebox3 post begin");
		PrintWriter writer = response.getWriter();
		String user_id = null;
		String my_story1 = null;
		while (er.hasMoreElements()) {
			String element = er.nextElement();

			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
				System.out.println("[vswb3] user_id:" + user_id);
			}

			if (element.equals("my_story")) {
				System.out.println("[vswb3] my_story into user_id:" + user_id);
				my_story1 = request.getParameterValues("my_story")[0];
			}
		}
		if ((user_id != null) && (my_story1 != null)) {
			HashMap<String, String> hm = new HashMap<String, String>();
			ResultSet rs = DB.select("select user_id from userinfo;");
			try {
				while (rs.next()) {
					System.out.println("[vswb3] rs user_id:" + rs.getString("user_id"));
					if (user_id.equals(rs.getString("user_id"))) {
						DB.update("update userinfo set my_story='" + my_story1 + "'  where user_id='" + user_id + "';");
						DB.update("update userinfo set time=now() where user_id='" + user_id + "';");
						writer.println("ok");
						return;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
//			System.out.println("[vswb3] my_story insert into user_id:" + my_story1);
//			DB.update("update userinfo set my_story='" + my_story1 + "'  where user_id=" + user_id + ";");
//			writer.println("ok");
		}
	}

}
